package com.iseastar.guojiang.newcabinet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.NewParcelBean;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;

/**
 * Created by joker on 2017/12/23.
 * 包裹拒收
 */
public class ParcelRejectActivity extends BaseActivity2 implements View.OnClickListener {

    private CheckBox mException1_CB;
    private CheckBox mException2_CB;
    private CheckBox mException3_CB;
    private CheckBox mException4_CB;
    private EditText mRemarkET;

    private NewParcelBean parcelBean = null;
    private int currentPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        parcelBean = (NewParcelBean) getIntent().getSerializableExtra("item");
        currentPosition = getIntent().getIntExtra("currentPosition", 0);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_parcel_reject);
        super.findViewById();
        getAppTitle().setCommonTitle("选择原因");
        mRemarkET = (EditText) findViewById(R.id.remark_et);
        mException1_CB = (CheckBox) findViewById(R.id.reject_checkbox1);
        mException1_CB.setOnClickListener(this);
        mException2_CB = (CheckBox) findViewById(R.id.reject_checkbox2);
        mException2_CB.setOnClickListener(this);
        mException3_CB = (CheckBox) findViewById(R.id.reject_checkbox3);
        mException3_CB.setOnClickListener(this);
        mException4_CB = (CheckBox) findViewById(R.id.reject_checkbox4);
        mException4_CB.setOnClickListener(this);
        findViewById(R.id.reject_item1).setOnClickListener(this);
        findViewById(R.id.reject_item2).setOnClickListener(this);
        findViewById(R.id.reject_item3).setOnClickListener(this);
        findViewById(R.id.reject_item4).setOnClickListener(this);

        findViewById(R.id.submit_bt).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //物品属于违禁品
            case R.id.reject_item1:
            case R.id.reject_checkbox1:
                mException1_CB.setChecked(true);
                mException2_CB.setChecked(false);
                mException3_CB.setChecked(false);
                mException4_CB.setChecked(false);
                break;
            //物品属于易碎品
            case R.id.reject_item2:
            case R.id.reject_checkbox2:
                mException1_CB.setChecked(false);
                mException2_CB.setChecked(true);
                mException3_CB.setChecked(false);
                mException4_CB.setChecked(false);
                break;
            //物品属于贵重物品
            case R.id.reject_item3:
            case R.id.reject_checkbox3:
                mException1_CB.setChecked(false);
                mException2_CB.setChecked(false);
                mException3_CB.setChecked(true);
                mException4_CB.setChecked(false);
                break;
            //其他
            case R.id.reject_item4:
            case R.id.reject_checkbox4:
                mException1_CB.setChecked(false);
                mException2_CB.setChecked(false);
                mException3_CB.setChecked(false);
                mException4_CB.setChecked(true);
                break;
            //提交
            case R.id.submit_bt:
                if (parcelBean != null) {
                    if (mException1_CB.isChecked()) {
                        showLoadingDialog(null);
                        AppHttp.getInstance().courierRejectionParcel(parcelBean.getDetailId(), 1, "");
                    } else if (mException2_CB.isChecked()) {
                        showLoadingDialog(null);
                        AppHttp.getInstance().courierRejectionParcel(parcelBean.getDetailId(), 2, "");
                    } else if (mException3_CB.isChecked()) {
                        showLoadingDialog(null);
                        AppHttp.getInstance().courierRejectionParcel(parcelBean.getDetailId(), 3, "");
                    } else if (mException4_CB.isChecked()) {
                        showLoadingDialog(null);
                        AppHttp.getInstance().courierRejectionParcel(parcelBean.getDetailId(), 4, mRemarkET.getText().toString());
                    } else {
                        showToast("请选择拒收原因");
                    }
                }
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.courier_rejection_parcel:
                try {
                    ReqResult<?> result = JSON.parser(msg.obj);
                    if (checkLoginStatus(result)) {
                        if ((Boolean) result.getResult()) {
                            showToast("包裹拒收成功");
                            parcelBean.setRejectStatus(true);
                            Intent intent = new Intent();
                            intent.putExtra("item", parcelBean);
                            intent.putExtra("currentPosition", currentPosition);
                            setResult(Activity.RESULT_OK, intent);
                            finish();
                        } else {
                            showToast("包裹拒收失败");
                        }
                    } else {
                        showToast(result.getMessage());
                    }
                } catch (Exception e) {
                    showToast("包裹拒收失败");
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }
}
